"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : 功能
Case Name   : 共享存储数据库主备切换的过程中kill待升主备节点进程
Create At   : 2023/04/19
Owner       : opentestcase043
Description :
        1.查询集群状态
        2.cm_ctl ddb写入键值对
        3.查询已插入键值对
        4.模糊查询已插入键值对
        5.删除已插入键值对
Expect      :
        1.集群状态正常
        2.成功
        3.返回已插入键值对对应的value值
        4.成功
        5.成功
History     :
"""

import os
import unittest

from testcase.utils.Constant import Constant
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Logger import Logger
from testcase.utils.Common import Common
from yat.test import Node

LOG = Logger()
Primary_SH = CommonSH('PrimaryDbUser')


@unittest.skipIf(1 == Primary_SH.get_node_num(), '单机环境不执行')
class ShareStorage(unittest.TestCase):

    def setUp(self):
        LOG.info('-----this is setup-----')
        self.com = Common()
        self.con = Constant()
        self.s1_node = Node('Standby1DbUser')
        self.s1_sh = CommonSH('Standby1DbUser')
        LOG.info(f'-----{os.path.basename(__file__)} start-----')

    def test_share_storage(self):
        text = '-----step1:查询集群状态; expect:集群状态正常-----'
        LOG.info(text)
        status = Primary_SH.exec_cm_ctl('query', '-Cv')
        LOG.info(status)
        self.assertIn('cluster_state   : Normal', status, '执行失败:' + text)
        res = status.splitlines()[-1].split('|')[0].split('6001')[1]
        self.assertIn('P Primary Normal', res, '执行失败:' + text)

        text = '-----step2:cm_ctl ddb写入键值对; expect:成功-----'
        LOG.info(text)
        status = Primary_SH.exec_cm_ctl('ddb', '--put 3 2')
        LOG.info(status)
        self.assertIn('exec ddb --put command success', status,
                      '执行失败:' + text)
        status = Primary_SH.exec_cm_ctl('ddb', '--put test test1')
        LOG.info(status)
        self.assertIn('exec ddb --put command success', status,
                      '执行失败:' + text)

        text = '-----step3:查询已插入键值对; expect:返回已插入键值对对应的value值-----'
        LOG.info(text)
        status = Primary_SH.exec_cm_ctl('ddb', '--get 3')
        LOG.info(status)
        self.assertIn('exec ddb --get command success', status,
                      '执行失败:' + text)

        text = '-----step4:模糊查询已插入键值对; expect:成功-----'
        LOG.info(text)
        status = Primary_SH.exec_cm_ctl('ddb', '--get 3')
        LOG.info(status)
        self.assertIn('exec ddb --get command success', status,
                      '执行失败:' + text)
        self.assertIn('2', status, '执行失败:' + text)

        text = '-----step5:删除已插入键值对; expect:成功-----'
        LOG.info(text)
        status = Primary_SH.exec_cm_ctl('ddb', '--delete 3')
        LOG.info(status)
        self.assertIn('exec ddb --delete command success', status,
                      '执行失败:' + text)
        status = Primary_SH.exec_cm_ctl('ddb', '--delete te --prefix')
        LOG.info(status)
        self.assertIn('exec ddb --delete command success', status,
                      '执行失败:' + text)

    def tearDown(self):
        text = '-----this is tearDown-----'
        LOG.info(text)
        LOG.info('-----无需清理环境-----')
        LOG.info(f'-----{os.path.basename(__file__)} end-----')
